Eureka注册客户端
2019-04-28 08:25:25来源:博客园 阅读 ()
1.pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.2.RELEASE</version> </dependency>
2.application.yaml
server: port: 8888 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=UTC username: root password: application: name: user-service mybatis: type-aliases-package: cn.itcast.user.pojo eureka: client: service-url: defaultZone: http://127.0.0.1:8886/eureka
3.入口文件 UserApplication.java
package cn.itcast; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import tk.mybatis.spring.annotation.MapperScan; @EnableDiscoveryClient @SpringBootApplication @MapperScan("cn.itcast.user.mapper") public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class); } }
4.UserController.java
package cn.itcast.user.controller; import cn.itcast.user.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController @RequestMapping("consumer") public class UserController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("{id}") public User queryUserById(@PathVariable("id") Long id){ List<ServiceInstance> instances = discoveryClient.getInstances("user-service"); ServiceInstance instance = instances.get(0); String url = "http://"+instance.getHost()+":"+instance.getPort()+"/user/" + id; User user = restTemplate.getForObject(url, User.class); return user; } }
原文链接:https://www.cnblogs.com/wuxiang12580/p/10784501.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- springboot~注册不同级别包里的bean~scanBasePackages 2020-06-10
- Spring Cloud Ribbon 客户端负载均衡 2020-06-04
- ahjesus-2020 Intellij idea 最新注册码 已稳定数月 亲测有 2020-05-25
- Spring Cloud微服务架构实战之Consul注册中心02:consul入门 2020-05-14
- java socket实现服务端,客户端简单网络通信。Chat 2020-05-03
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash