spring boot 通过Eureka搭建微服务

一、构建一个micro-parent01项目,下面创建3个quickstart

ps:micro-parent01的pom.xml文件在最后面




二、micro-registry 注册中心搭建

1、application.yml 全局配置

server:
  port:8080

eureka:
  instance:
    hostname: localhost
  client: #not a client,don't register with yourself
    registerWithEureka: false
    fetchRegistry: false
注意:
client: #not a client,don't register with yourself
registerWithEureka: false
fetchRegistry: false
如果不加上的话会报错:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
而且 registerWithEureka和fetchRegistry的冒号后面也要空一格空格才不会出错,就是该两个单词需要改变成关键字的颜色才不会出错。
2、编写启动程序
 
@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {

    public static void main(String[] args) {

        SpringApplication.run(RegistryApplication.class);
    }
}


3、main方法启动,通过网页:localhost:8080启动查看


三、向注册中心注册(提供)服务

1、配置application.yml文件(该文件实在resources下面)

# HTTP (Tomcat) port
server:
  port: 8081

#Application name
spring:
  application:
    name: micro-service

# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
2、基于RESTful风格编写一个服务并暴露
@RestController
public class HelloWorldController {
    @RequestMapping("hello/{name}")
    public String hello(@PathVariable String name) {
        return name + " say hello";
    }

}
3、编写启动程序
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceApplication {

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

}
4、启动成功

5、网页刷新http://localhost:8080

此时我们就会发现服务已经注册到了注册中心

四 micro-client端发现(调用)服务
1、配置文件application.yml
# HTTP (Tomcat) port
server:
  port: 8082

#Application name
spring:
  application:
    name: micro-client

# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

# application parameters
app:
  service-url: http://MICRO-SERVICE/


2、编写 Controller层
@RestController
public class CallHelloController {
	
	@Autowired
	private CallHelloService callHelloService;
	
	@GetMapping("hello")
	public String hello(String name) {
		String result = callHelloService.callHello(name);
		return result;
	}
	
}

3、编写service层,通过service来调用服务
@Service
public class CallHelloService {

    @Value("${app.service-url}")
    private String appServiceUrl;

    @Autowired
    private RestTemplate restTemplate;

    public String callHello(String name) {
        // 是一个http client
        ResponseEntity
      
      
       
        result = restTemplate.postForEntity(appServiceUrl + "hello/" + name, null, String.class);
        return result.getBody();
    }
}

      
      
注意: RestTemplate是spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http的方法,能够大大提高客户的编写效率。调用RestTemplate的默认构造函数,RestTemplate对象在底层通过java.net包下面的实现创建HTTP请求,可以通过ClientHttpRequestFactory指定不同的Http请求方式。
ClientHttpRequestFactory接口主要提供了两种实现方式:
一种是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接。
另一种方式是使用HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务,使用HttpClient可以配置连接池和证书等信息。
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#rest-resttemplate


4、编写启动程序
@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {

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


    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
@LoadBalanced 注解开启均衡负载能力

5、网页启动成功截图


6、通过localhost:8082/hello?name=ycx能得到下面的,就证明已经搭建成功了


micro-parent01的pom.xml文件


     
     
  
      
      
       
       4.0.0
      
      

  
      
      
       
       com.usst
      
      
  
      
      
       
       micro-parent01
      
      
  
      
      
       
       1.0-SNAPSHOT
      
      
  
      
      
    
       
       
        
        micro-client
       
       
    
       
       
        
        micro-registry
       
       
    
       
       
        
        micro-service
       
       
  
      
      
  
      
      
       
       pom
      
      

  
      
      
    
       
       
      
        
        
         
         website
        
        
      
        
        
         
         scp://webhost.company.com/www/website
        
        
    
       
       
  
      
      

  
      
      
    
       
       
        
        UTF-8
       
       
    
       
       
        
        UTF-8
       
       
  
      
      

  
      
      
    
       
       
        
        org.springframework.cloud
       
       
    
       
       
        
        spring-cloud-starter-parent
       
       
    
       
       
        
        Camden.SR6
       
       
  
      
      

  
      
      

    
       
       
    
       
       
      
        
        
         
         org.springframework.cloud
        
        
      
        
        
         
         spring-cloud-starter
        
        
    
       
       

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

    
       
       
      
        
        
         
         junit
        
        
      
        
        
         
         junit
        
        
      
        
        
         
         test
        
        
    
       
       

  
      
      

  
      
      
    
       
       
      
        
        
      
        
        
        
         
         
           org.apache.maven.plugins 
         
        
         
         
           maven-compiler-plugin 
         
        
         
         
           2.3.2 
         
        
         
          
          1.7 
          
            1.7 
           
          
            UTF-8 
           
           
           
             ${java.home}/lib/rt.jar 
            
           
         
      
        
        

    
       
       
  
      
      


     
     


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值