Spring Cloud(二):Eureka服务注册与发现

   搭建Maven项目:

一:创建父工程:cloud-demo

pom文件如下:



	4.0.0

	spring-cloud-demo
	cloud-demo
	0.0.1-SNAPSHOT
	pom
	
		eureka-server
		eureka-client
		eureka-consumer
		eureka-feign
	
	cloud-demo
	Demo project for Spring Boot

	
		UTF-8
		UTF-8
		1.8
		Finchley.RELEASE
	
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.1.RELEASE
	
	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.RELEASE
				pom
				import
			
		
	
	
		
			org.springframework.cloud
			spring-cloud-starter-config
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



二:创建module:eureka-server服务注册管理中心:

pom文件如下:



	4.0.0

	eureka-server
	0.0.1-SNAPSHOT
	jar

	eureka-server
	Demo project for Spring Boot

	
		spring-cloud-demo
		cloud-demo
		0.0.1-SNAPSHOT
	



	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		
	



application.yml配置如下:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

三:创建服务提供者module模块:eureka-client

pom文件如下:



	4.0.0

	eureka-client
	0.0.1-SNAPSHOT
	jar

	eureka-client
	Demo project for Spring Boot

	
		spring-cloud-demo
		cloud-demo
		0.0.1-SNAPSHOT
		 
	


	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		
	



application.yml配置如下:

server:
  port: 8080
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: service-provider

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}
}

创建服务提供类:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HiController {

    @GetMapping("/provider")
    public String home(@RequestParam String name){
        return "hi " + name + ",i am from provider: " + name;
    }
}

运行项目:访问服务注册中心(我这里是localhost:8761/eureka)

Spring Cloud(二):Eureka服务注册与发现_第1张图片

可以看见服务发布成功。

四:创建服务消费者(方式一:基于ribbon调用服务):eureka-consumer

pom文件如下:




    
        cloud-demo
        spring-cloud-demo
        0.0.1-SNAPSHOT
    
    4.0.0

    eureka-consumer
    jar

    
        
            org.springframework.cloud
            spring-cloud-netflix-ribbon
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    


application.yml配置:

server:
  port: 8090
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: service-consumer
debug: true

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaRibbonApplication.class, args);
    }
}

service:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    public String hello(String name){
        return restTemplate.getForObject("http://SERVICE-PROVIDER/provider?name=" + name, String.class);
    }
}

RestTemplate配置类:
 

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfiguration {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

控制层:

import com.qinker.ribbon.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;


    @RequestMapping("/hi")
    public String hello(String name){
        return helloService.hello(name);
    }
}

启动:访问:localhost:8090/hi?name=xxx

五:创建服务消费者(二:使用fegin方式)

pom文件如下:




    
        cloud-demo
        spring-cloud-demo
        0.0.1-SNAPSHOT
    
    4.0.0

    eureka-feigon

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

application.yml:

server:
  port: 8091
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: client-feign

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientFeignApplication.class, args);
    }
}

service:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "SERVICE-PROVIDER")
public interface HelloService {

    @RequestMapping(value = "/provider", method = RequestMethod.GET)
    String hello(@RequestParam(value = "name") String name);
}

controller:

import com.eureka.feign.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/feign")
    public String hello(String name){
        return helloService.hello(name);
    }
}

启动:访问:http://localhost:8091/feign?name=zhang

Spring Cloud(二):Eureka服务注册与发现_第2张图片

你可能感兴趣的:(Spring,Cloud)