SpringBoot整合Eureka搭建微服务
程序员文章站
2022-06-04 07:58:12
1.创建一个services项目,添加三个子模块client(客户端)、service(服务端)、registry(注册中心) 1.1 创建一个services项目 1.2 添加pom.xml依赖 1.3 添加子模块client(客户端) 1.4 添加子模块service(服务端) 1.5 添加子模 ......
1.创建一个services项目,添加三个子模块client(客户端)、service(服务端)、registry(注册中心)
1.1 创建一个services项目
1.2 添加pom.xml依赖
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <packaging>pom</packaging> <modules> <module>client</module> <module>service</module> <module>registry</module> </modules> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.1.2.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.micro</groupid> <artifactid>services</artifactid> <version>0.0.1-snapshot</version> <name>services</name> <description>services</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid> <version>1.4.5.release</version> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>finchley.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> </project>
1.3 添加子模块client(客户端)
1.4 添加子模块service(服务端)
1.5 添加子模块registry(注册中心)
2.搭建registry(注册中心)
2.1 application.yml 配置
server: port: 8080 eureka: instance: hostname: localhost client: registerwitheureka: false fetchregistry: false
2.2 编写启动程序
package com.services.registry; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.netflix.eureka.server.enableeurekaserver; @springbootapplication @enableeurekaserver public class registryapplication { public static void main(string[] args) { springapplication.run(registryapplication.class); } }
3.搭建service(服务端)
3.1 application.yml 配置
server: port: 8081 spring: application: name: service eureka: client: serviceurl: defaultzone: http://localhost:8080/eureka/
3.2 编写启动程序
package com.services.service; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; @springbootapplication @enablediscoveryclient public class serviceapplication { public static void main(string[] args) { springapplication.run(serviceapplication.class, args); } }
3.3 编写服务
package com.services.service; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class helloworldcontroller { @requestmapping("hello/{name}") public string hello(@pathvariable string name) { return name + " say hello"; } }
4.搭建client(客户端)
4.1 application.yml 配置
server: port: 8082 spring: application: name: client eureka: client: serviceurl: defaultzone: http://localhost:8080/eureka/ app: service-url: http://localhost:8081/
4.2 编写启动程序
package com.services.client; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; import org.springframework.context.annotation.bean; import org.springframework.web.client.resttemplate; @springbootapplication @enablediscoveryclient public class clientapplication { public static void main(string[] args) { springapplication.run(clientapplication.class, args); } @bean resttemplate resttemplate() { return new resttemplate(); } }
4.3 编写service
package com.services.client; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.http.responseentity; import org.springframework.stereotype.service; import org.springframework.web.client.resttemplate; @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().tostring(); } }
4.4 编写controller
package com.services.client; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class callhellocontroller { @autowired private callhelloservice callhelloservice; @getmapping("hello") public string hello(string name) { string result = callhelloservice.callhello(name); return result; } }
5.启动registry-service-client模块
5.1 registry
http://localhost:8080/
5.2 service
刷新网页 http://localhost:8080/
5.3 client
刷新网页 http://localhost:8080/
client 访问 http://localhost:8082/hello?name=tao
源码地址: https://github.com/80905949/springcloud.git
上一篇: js 控制文本框中特殊字符的输入
下一篇: R语言-如何读取前n行数据
推荐阅读
-
详解mall整合SpringBoot+MyBatis搭建基本骨架
-
springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)
-
【原】无脑操作:Gitblit服务器搭建及IDEA整合Git使用
-
SpringBoot2 整合 Zookeeper组件,管理架构中服务协调
-
SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用
-
微信小程序搭建自己的Https服务器
-
微信小程序访问node.js接口服务器搭建教程
-
SpringBoot整合SpringCloud搭建分布式应用
-
SpringBoot2 整合Nacos组件,环境搭建和入门案例详解
-
Springboot整合微信小程序实现登录与增删改查