spring cloud 之 server-provider
2017-07-29 22:15
288 查看
项目名称 ms-provider-user
1.pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.djl.springcloud</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ms-provider-user</artifactId>
<packaging>jar</packaging>
<name>ms-provider-user</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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-eureka</artifactId>
</dependency>
</dependencies>
</project>
2./ms-provider-user/src/main/resources/application.yml
server:
port: 8011
spring:
application:
name: ms-provider-user
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ instance:
preferIpAddress: true
3.终于到了写服务的时候了
简单来个例子
package com.djl.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.djl.provider.model.User;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/getUser")
public User getUser() {
User user = new User();
user.setId(11L);
user.setName("dingjianglei");
return user;
}
}
User.java
package com.djl.provider.model;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.main入口
package com.djl.provider;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ProviderApplication.class).web(true).run(args);
}
}
@EnableDiscoveryClient 注解用于服务发现 在 eureka 页面可以看到服务
5.看看服务注册上去没有
看到红色部分说明服务注册成功了~
顺便验证一直服务
服务科访问 OK~
1.pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.djl.springcloud</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ms-provider-user</artifactId>
<packaging>jar</packaging>
<name>ms-provider-user</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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-eureka</artifactId>
</dependency>
</dependencies>
</project>
2./ms-provider-user/src/main/resources/application.yml
server:
port: 8011
spring:
application:
name: ms-provider-user
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ instance:
preferIpAddress: true
3.终于到了写服务的时候了
简单来个例子
package com.djl.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.djl.provider.model.User;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/getUser")
public User getUser() {
User user = new User();
user.setId(11L);
user.setName("dingjianglei");
return user;
}
}
User.java
package com.djl.provider.model;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.main入口
package com.djl.provider;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ProviderApplication.class).web(true).run(args);
}
}
@EnableDiscoveryClient 注解用于服务发现 在 eureka 页面可以看到服务
5.看看服务注册上去没有
看到红色部分说明服务注册成功了~
顺便验证一直服务
服务科访问 OK~
相关文章推荐
- spring-cloud中遇到的坑sidecar与config server client
- spring cloud中启动Eureka Server的方法
- 为Spring Cloud Config Server配置远程git仓库
- spring-cloud入门之eureka-server(服务发现)
- 学习Spring Cloud第六课(将微服务注册到Eureka Server上)
- (2-2)SpringCloud-服务注册到Eureka Server集群并消费
- Spring Cloud Config Server 配置总结
- 为Spring Cloud Config Server配置远程git仓库
- java.io.FileNotFoundException: class path resource [templates/] spring-cloud-netflix-eureka-server
- [Spring Cloud-01] eureka server 服务注册与发现
- (01) SpringCloud: Eureka Server 搭建
- spring-cloud-config-server——Environment Repository(File System Backend)
- springCloud之Eureka Server集群
- SpringCloud问题解决:spring-cloud-eureka启动出错Cannot execute request on any known server
- spring cloud 的启动Eureka client报错--Cannot execute request on any known server
- spring cloud:config-server中@RefreshScope的"陷阱"
- spring cloud-给Eureka Server加上安全的用户认证
- springcloud(二):spring cloud eureka 注册中心server 启动
- SpringCloud Config Server中{application}等占位符使用场景设置默认拉去分支
- spring cloud config-server 高可用配置中心